package com.taptech.doufu.services;

import android.content.Context;
import android.content.Intent;

import com.taptech.doufu.ui.activity.GiftUserListActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.bean.ProductionBean;
import com.taptech.doufu.constant.GlobalConfig;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpUtil;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by longsiyang on 16/1/8.
 */
public class GiftService {
    public static final String BASE_URL = "http://" + Constant.HOST_NAME
            + "/index.php/";
    static final public int HANDLE_LOAD_GIFT_LIST = 4001;
    static final public int HANDLE_LOAD_GIFT_RANK_LIST = 4002;
    static final public int HANDLE_GIVE_GIFT_REQUEST = 4003;

    private static GiftService instance = new GiftService();

    private GiftService() {
    }

    public static GiftService getInstance() {
        return instance;
    }

    public static void enterGiftListActivity(Context context , ProductionBean bean){
        Intent intent = new Intent(context , GiftUserListActivity.class);
        intent.putExtra(Constant.DATA , bean);
        context.startActivity(intent);
    }

    public static void enterPayWebActivity(Context context){
        if (AccountService.getInstance().isLogin()){
            if (GlobalConfig.isGooglePlay) {
                SimpleWeexActivity.startActivity(context,"vue/personalcenter/TFMyCoinForGoogle.js?needPay=1");
            }else{
                SimpleWeexActivity.startActivity(context,"vue/personalcenter/TFMyCoin.js");
            }
        }else {
            AccountService.getInstance().jumpToLogin();
        }
    }

    public static void enterRuchWebActivity(Context context){
        if (AccountService.getInstance().isLogin()){
            Intent intent = new Intent(context , BrowseActivity.class);
            intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/journal/detail?id=110");
            context.startActivity(intent);
        }else {
            AccountService.getInstance().jumpToLogin();
        }

    }


    public void loadGiftList(HttpResponseListener listener, String object_type){
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_LOAD_GIFT_LIST);
        request.setUrl(BASE_URL + "gifts/index?object_type=" + object_type);
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    public void loadGiftRankList(HttpResponseListener listener , String id , String object_type){
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_LOAD_GIFT_RANK_LIST);
        request.setUrl(BASE_URL + "rewardrecords/top?object_type=" + object_type + "&object_id=" + id );
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }

    public void sendGiftRequest(HttpResponseListener listener , String object_id , String object_type , String gift_id , String gift_num){
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        NameValuePair value;
        value = new BasicNameValuePair("object_id", object_id);
        list.add(value);
        value = new BasicNameValuePair("object_type", object_type);
        list.add(value);
        value = new BasicNameValuePair("gift_id", gift_id);
        list.add(value);
        value = new BasicNameValuePair("count", gift_num);
        list.add(value);
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_GIVE_GIFT_REQUEST);
        request.setUrl(Constant.HOST_NAME_SECOND + "/index.php/rewardrecords/reward");
        request.setListener(listener);
        request.setData(list);
        HttpUtil.sendPostRequest(request);

    }

}
